package com.examination.auth.controller;


import com.examination.auth.service.UserService;
import com.examination.domain.auth.entity.User;
import com.examination.domain.common.ResponseResult;
import com.examination.enums.AppHttpCodeEnum;
import com.examination.exception.MySystemException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public ResponseResult login(@RequestBody User user){
        if (Objects.isNull(user)){
            //判断是否为空，前后端需要都做验证，保证安全
            throw new MySystemException(AppHttpCodeEnum.REQUIRE_USERNAME);
        }
        return userService.login(user);
    }

    @PostMapping("/register")
    public ResponseResult register(@RequestBody User user){
        if (Objects.isNull(user)){
            //判断是否为空，前后端需要都做验证，保证安全
            throw new MySystemException(AppHttpCodeEnum.REQUIRE_USERNAME);
        }
        return userService.register(user);
    }

    @GetMapping("userInfo")
    public  ResponseResult getUserInfo(){
        return userService.getUserInfo();
    }
}
